package br.com.ViewConfiguracoes;

import br.com.TelaPrincipal.Fr_Principal;
import br.com.controler.CaixaControler;
import br.com.controler.ConfiguracaoControler;
import br.com.modelo.Caixa;
import br.com.modelo.Configuracao;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import util.Utilitario;

/**
 *
 * @author Administrador
 */
public class Fr_Configuracao extends javax.swing.JInternalFrame {

    private static Configuracao configuracao;
    private static List<Caixa> listCaixa = new ArrayList<>();

    public Fr_Configuracao() {
        initComponents();
        Utilitario.bloqueiaTodosCampos(rootPane);
        comboCaixas.setEnabled(true);
        populaComboCaixas();

        //atalhso botões
        CancelaAction cancelaAction = new CancelaAction();
        rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancelaAction");
        rootPane.getActionMap().put("cancelaAction", cancelaAction);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        editIp = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        comboCaixas = new javax.swing.JComboBox();

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Servidor"));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("IP: ");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 30, 20));
        jPanel2.add(editIp, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 20, 140, -1));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 60, 300, 80));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/imgBotoes/botaoCancelar.png"))); // NOI18N
        jButton1.setText("Fechar (ESC)");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 280, 120, -1));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Selecione o caixa"));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        comboCaixas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCaixasActionPerformed(evt);
            }
        });
        jPanel3.add(comboCaixas, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 230, -1));

        jPanel1.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 260, 60));

        jTabbedPane1.addTab("Configurações", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void comboCaixasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCaixasActionPerformed
        //pegando o caixa selecionado
        String ip = null;
        for (Caixa c : listCaixa) {
            if (comboCaixas.getSelectedItem().toString().contains(c.getNome())) {
                ip = c.getIp();
                break;
            }
        }
        if (ip != null) {
            ConfiguracaoControler confControl = new ConfiguracaoControler();
            configuracao = confControl.pegaConfiguracaoCaixa(ip);
            populaFormConfiguracao();
        }
    }//GEN-LAST:event_comboCaixasActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboCaixas;
    private javax.swing.JTextField editIp;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

    private void populaFormConfiguracao() {
        if (configuracao != null) {
            editIp.setText(configuracao.getIdEcfCaixa().getIp());
        }
    }

    private void populaComboCaixas() {
        listCaixa = CaixaControler.retornaCaixas();
        for (Caixa c : listCaixa) {
            comboCaixas.addItem(c.getId() + ": " + c.getNome() + " - " + c.getIp());
        }
    }

    private class CancelaAction extends AbstractAction {

        public CancelaAction() {
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Fr_Principal.configuracao = null;
            dispose();
        }
    }
}
